// LibrarySystem.scala

// Book 类
case class Book(title: String, author: String, isbn: String)

// Library 类
class Library {
  private var availableBooks: List[Book] = List()
  private var borrowedBooks: List[Book] = List()

  // 添加新书
  def addBook(book: Book): Unit = {
    availableBooks = availableBooks :+ book
    println(s"Added book: ${book.title}")
  }

  // 借出书籍
  def borrowBook(isbn: String): Option[Book] = {
    availableBooks.find(book => book.isbn == isbn) match {
      case Some(book) =>
        availableBooks = availableBooks.filterNot(_.isbn == isbn)
        borrowedBooks = borrowedBooks :+ book
        println(s"Book borrowed: ${book.title}")
        Some(book)
      case None =>
        println(s"Book with ISBN $isbn is not available.")
        None
    }
  }

  // 归还书籍
  def returnBook(isbn: String): Option[Book] = {
    borrowedBooks.find(book => book.isbn == isbn) match {
      case Some(book) =>
        borrowedBooks = borrowedBooks.filterNot(_.isbn == isbn)
        availableBooks = availableBooks :+ book
        println(s"Book returned: ${book.title}")
        Some(book)
      case None =>
        println(s"Book with ISBN $isbn is not borrowed.")
        None
    }
  }

  // 查看所有可借书籍
  def listAvailableBooks(): Unit = {
    if (availableBooks.isEmpty) {
      println("No available books.")
    } else {
      println("Available books:")
      availableBooks.foreach(book => println(s"${book.title} by ${book.author} (ISBN: ${book.isbn})"))
    }
  }

  // 查看所有已借书籍
  def listBorrowedBooks(): Unit = {
    if (borrowedBooks.isEmpty) {
      println("No borrowed books.")
    } else {
      println("Borrowed books:")
      borrowedBooks.foreach(book => println(s"${book.title} by ${book.author} (ISBN: ${book.isbn})"))
    }
  }
}

// 主程序
object BookManagementSystem {
  def main(args: Array[String]): Unit = {
    val library = new Library

    val book1 = Book("1984", "George Orwell", "978-0451524935")
    val book2 = Book("To Kill a Mockingbird", "Harper Lee", "978-0061120084")

    library.addBook(book1)
    library.addBook(book2)

    library.listAvailableBooks()

    library.borrowBook("978-0451524935")

    library.listBorrowedBooks()

    library.returnBook("978-0451524935")

    library.listAvailableBooks()
  }
}
